﻿
using System;
using System.Windows;
using System.Windows.Controls;
using MahApps.Metro;

namespace Red.Controls.Shell
{
    public class RedApplication : Application
    {
        #region Static methods

        public static Canvas FindCanvasResource( string name )
        {
            return (Canvas)Application.Current.FindResource( name );
        }

        #endregion

        #region C-tor

        public RedApplication()
        {
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" ) } );
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" ) } );
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" ) } );
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/MahApps.Metro;component/Styles/Accents/Crimson.xaml" ) } );
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" ) } );
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/MahApps.Metro.Resources;component/Icons.xaml" ) } );
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/Red.Resources;component/RedIcons.xaml" ) } );
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/Red.Controls;component/Themes/Red.xaml" ) } );
            Resources.MergedDictionaries.Add( new ResourceDictionary() { Source = new Uri( "pack://application:,,,/Red.Controls;component/TimeLine/TimeLineResources.xaml" ) } );

            UpdateDynamicStyles();
        }

        #endregion C-tor

        #region Public methods

        public void ChangeAccentColor( string accentColor )
        {
            var currentStyle = ThemeManager.DetectAppStyle( this );
            var newAccent = ThemeManager.GetAccent( accentColor );
            ThemeManager.ChangeAppStyle( this, newAccent, currentStyle.Item1 );
        }

        public void ChangeAppTheme( string appTheme )
        {
            var currentStyle = ThemeManager.DetectAppStyle( this );
            var newTheme = ThemeManager.GetAppTheme( appTheme );
            ThemeManager.ChangeAppStyle( this, currentStyle.Item2, newTheme );
            UpdateDynamicStyles();
        }

        #endregion Public methods

        #region Private Methods

        private void UpdateDynamicStyles()
        {
            var currentStyle = ThemeManager.DetectAppStyle( this );

            if ( currentStyle.Item1.Name == "BaseDark" )
            {
                Resources[ "CamouflageButton" ] = FindResource( "DarkMetroWindowButtonStyle" );
            }
            else
            {
                Resources[ "CamouflageButton" ] = FindResource( "LightMetroWindowButtonStyle" );
            }
        }

        #endregion Private Methods
    }
}
